package com.ssm.gbq.controller;

import com.ssm.gbq.mapper.DoctorMapper;
import com.ssm.gbq.model.Doctor;
import com.ssm.gbq.service.DoctorService;
import gbq.ssm.utils.DateUtil;
import gbq.ssm.utils.PageBounds;
import gbq.ssm.utils.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/doctor")
public class DoctorController {
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private DoctorMapper doctorMapper;

    @ResponseBody
    @RequestMapping(value = "/openDoctorTable", method = RequestMethod.POST)
    public HashMap<String, Object> openDoctorTable(String doctorName, String createTimeMax, String createTimeMin, int pageSize, int currentPage) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        PageBounds<Doctor> pageBounds = null;
        try {
            pageBounds = doctorService.openDoctorTable(doctorName, createTimeMax, createTimeMin, currentPage, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (doctorName != null && doctorName != "" || createTimeMax != null && createTimeMax != "" || createTimeMin != null && createTimeMin != "") {
            result.put("count", pageBounds.getPageList().size());
        } else {
            result.put("count", doctorMapper.Count());
        }
        result.put("data", pageBounds);
        return result;
    }

    /**
     * 根据类别获取实体数量
     */
    @ResponseBody
    @RequestMapping(value = "/findByType", method = RequestMethod.POST)
    public HashMap<String, Object> findByType(Integer[] tid) {
        HashMap<String, Object> result = new HashMap<>();
        int num = doctorMapper.findByType(Arrays.asList(tid));
        result.put("doctorNum", num);
        return result;
    }

    /**
     * 根据id获取实体
     */
    @ResponseBody
    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    public HashMap<String, Object> findById(Integer id) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        Doctor doctor = doctorService.findById(id);
        result.put("doctor", doctor);
        return result;
    }

    /**
     * 获取全部信息
     */
    @ResponseBody
    @RequestMapping(value = "/selectAllDoctor", method = RequestMethod.POST)
    public HashMap<String, Object> selectAllDoctor() {
        HashMap<String, Object> result = new HashMap<String, Object>();
        List<Doctor> doctor = doctorService.selectAllDoctor();
        result.put("data", doctor);
        return result;
    }

    /**
     * 添加
     */
    @ResponseBody
    @RequestMapping(value = "/addDoctor", method = RequestMethod.POST)
    public HashMap<String, Object> addDoctor(Doctor doctor) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        doctor.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        doctorService.addDoctor(doctor);
        return result;
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping(value = "/updateDoctor", method = RequestMethod.POST)
    public HashMap<String, Object> updateDoctor(Doctor doctor) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        doctorService.updateDoctor(doctor);
        return result;
    }

    /**
     * 通过id删除
     */
    @ResponseBody
    @RequestMapping(value = "/deleteDoctorById", method = RequestMethod.POST)
    public HashMap<String, Object> deleteDoctorById(Integer id) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        // DoctorService.deleteDoctorById(Arrays.asList(id));
        doctorService.deleteDoctorById(id);
        return result;
    }

    /**
     * 通过id多删除
     *
     * @param @return
     * @Title: delStudentById
     */
    @ResponseBody
    @RequestMapping(value = "/deleteDoctorByIds", method = RequestMethod.POST)
    public HashMap<String, Object> deleteDoctorByIds(Integer[] ids) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        doctorService.deleteDoctorByIds(Arrays.asList(ids));
        return result;
    }

    /**
     * 上传图片
     */
    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public HashMap<String, Object> uploadImg(@RequestParam MultipartFile name, HttpServletRequest request) {
        HashMap<String, Object> result = new HashMap<String, Object>();

        if (name.isEmpty()) {
            result.put("status", 400);
            result.put("Msg", "文件为空，请重新上传");
            return result;
        }


        try {
            byte[] bytes = name.getBytes();
            String imageName = UUID.randomUUID().toString();
            try {
                //使用base64方式上传到七牛云
                String url = QiniuCloudUtil.put64image(bytes, imageName);
                String urlimg = QiniuCloudUtil.getDOMAIN() + "/" + imageName;
                result.put("status", 200);
                result.put("Msg", "文件上传成功");
                result.put("url", urlimg);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            result.put("status", 500);
            result.put("Msg", "文件上传发生异常！");
        } finally {
            return result;
        }
    }

    /**
     * 保存图片信息
     */
    @ResponseBody
    @RequestMapping(value = "/SaveImg", method = RequestMethod.POST)
    public HashMap<String, Object> SaveImg(Doctor doctor) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        doctorService.updateDoctor(doctor);
        return result;
    }

    /**
     * 删除云端图片
     */
   /* @ResponseBody
    @RequestMapping(value = "/DelImg", method = RequestMethod.POST)
    public HashMap<String, Object> DelImg(String imgUrl) {
        HashMap<String, Object> result = new HashMap<String, Object>();
        try {
            QiniuCloudUtil.delete(imgUrl);
        }   catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }*/
}
